package com.budwk.app.sqfw.services.datamange.impl;

import cn.hutool.core.bean.BeanUtil;
import com.budwk.app.common.config.AppException;
import com.budwk.app.sqfw.constants.CommonForAdminConstants;
import com.budwk.app.sqfw.models.Sqfw_datamange_community;
import com.budwk.app.sqfw.services.SqfwFormsummaryNewService;
import com.budwk.app.sqfw.services.datamange.DataMangeCityCheckService;
import com.budwk.app.sqfw.services.datamange.DataMangeDistrictService;
import com.budwk.app.sqfw.services.datamange.DataMangeMainService;
import com.budwk.app.sqfw.services.datamange.DataMangeProvinceCheckService;
import com.budwk.app.sqfw.vo.SqfwDataMangeCheckVO;
import com.budwk.app.sqfw.vo.SqfwDataMangeVO;
import com.budwk.app.sys.models.LinuxPackageLay;
import com.budwk.app.sys.models.Sys_user;
import com.budwk.app.sys.services.SysUserService;
import com.budwk.starter.common.page.Pagination;
import com.budwk.starter.security.utils.SecurityUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.nutz.dao.Cnd;
import org.nutz.dao.sql.Criteria;
import org.nutz.dao.util.cri.SqlExpressionGroup;
import org.nutz.dao.util.cri.Static;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;

@Slf4j
@IocBean
public class DataMangeMainServiceImpl implements DataMangeMainService {
    @Inject
    private SysUserService sysUserService;
    @Inject
    private SqfwFormsummaryNewService sqfwFormsummaryNewService;
    @Inject
    private DataMangeDistrictService dataMangeDistrictService;
    @Inject
    private DataMangeCityCheckService dataMangeCityService;
    @Inject
    private DataMangeProvinceCheckService dataMangeProvinceCheckService;

    @Override
    public Pagination page(SqfwDataMangeVO bean) {
        if (null == bean) {
            bean = new SqfwDataMangeVO();
        }
        long startTime = System.currentTimeMillis();
        // 构造查询条件
        Criteria criteria = buildQueryCond(bean);
        log.info("DataMangeCheckServiceImpl-list-SQL: {}", criteria.toString());

        String areaLevel = checkUserArea();
        if (StringUtils.isEmpty(areaLevel)) {
            throw new AppException("用户行政区域参数错误!");
        }
        Pagination pagination = new Pagination();//sqfwFormsummaryNewService.listPage(bean.getPageNo(), bean.getPageSize(), criteria);
        switch (areaLevel) {
            case CommonForAdminConstants.PROVINCE:
                pagination = dataMangeProvinceCheckService.listPage(bean.getPageNo(), bean.getPageSize(), criteria);
                break;
            case CommonForAdminConstants.CITY:
                pagination = dataMangeCityService.listPage(bean.getPageNo(), bean.getPageSize(), criteria);
                break;
            case CommonForAdminConstants.DISTRICT:
                pagination = dataMangeDistrictService.listPage(bean.getPageNo(), bean.getPageSize(), criteria);
                break;
            case CommonForAdminConstants.COUNTY:
                pagination = sqfwFormsummaryNewService.listPage(bean.getPageNo(), bean.getPageSize(), criteria);
                break;
        }
        log.info("DataMangeCheckServiceImpl-list-耗时: {}ms", System.currentTimeMillis() - startTime);
        return pagination;
    }

    @Override
    public Pagination pageSelf(SqfwDataMangeVO bean) {
        if (null == bean) {
            bean = new SqfwDataMangeVO();
        }
        long startTime = System.currentTimeMillis();
        String areaLevel = checkUserArea();
        if (StringUtils.isEmpty(areaLevel)) {
            throw new AppException("用户行政区域参数错误!");
        }
        Pagination pagination = new Pagination();
        switch (areaLevel) {
            case CommonForAdminConstants.PROVINCE:
                pagination = new Pagination();
                break;
            case CommonForAdminConstants.CITY:
                SqfwDataMangeCheckVO vo = new SqfwDataMangeCheckVO();
                BeanUtil.copyProperties(bean, vo);
                pagination = dataMangeProvinceCheckService.page(vo);
                break;
            case CommonForAdminConstants.DISTRICT:
                SqfwDataMangeCheckVO vo1 = new SqfwDataMangeCheckVO();
                BeanUtil.copyProperties(bean, vo1);
                pagination = dataMangeCityService.page(vo1);
                break;
            case CommonForAdminConstants.COUNTY:
            case CommonForAdminConstants.COMMUNITY:
                pagination = dataMangeDistrictService.page(bean);
                break;
        }
        log.info("DataMangeCheckServiceImpl-list-耗时: {}ms", System.currentTimeMillis() - startTime);
        return pagination;
    }

    /**
     * 检查当前登录用户的行政区域级别
     *
     * @return
     */
    private String checkUserArea() {
        Sys_user sysUser = sysUserService.fetch(SecurityUtil.getUserId());
        if (StringUtils.isEmpty(sysUser.getProvince()) || CommonForAdminConstants.ZHISHU.equals(sysUser.getProvince())) {
            return CommonForAdminConstants.PROVINCE;
        } else if (StringUtils.isEmpty(sysUser.getCity()) || CommonForAdminConstants.ZHISHU.equals(sysUser.getCity())) {
            return CommonForAdminConstants.PROVINCE;
        } else if (StringUtils.isEmpty(sysUser.getDistrict()) || CommonForAdminConstants.ZHISHU.equals(sysUser.getDistrict())) {
            return CommonForAdminConstants.CITY;
        } else if (StringUtils.isEmpty(sysUser.getCounty()) || CommonForAdminConstants.ZHISHU.equals(sysUser.getCounty())) {
            return CommonForAdminConstants.DISTRICT;
        } else if (StringUtils.isEmpty(sysUser.getCommunity()) || CommonForAdminConstants.ZHISHU.equals(sysUser.getCommunity())) {
            return CommonForAdminConstants.COUNTY;
        }
        return "";
    }

    /**
     * 构造查询条件
     *
     * @param bean
     * @return
     */
    private Criteria buildQueryCond(SqfwDataMangeVO bean) {
        Criteria cri = Cnd.cri();
        cri.getOrderBy().desc(SqfwDataMangeVO::getCreateDate);
        SqlExpressionGroup criSql = cri.where();
        // 全局搜索
        if (org.apache.commons.lang.StringUtils.isNotEmpty(bean.getQueryCondition())) {
            SqlExpressionGroup placeName = Cnd.exps(new Static(" param_data->>'placeName' like '%" + bean.getQueryCondition() + "%'"));
            SqlExpressionGroup placeType = Cnd.exps(new Static(" param_data->>'placeType' like '%" + bean.getQueryCondition() + "%'"));
            SqlExpressionGroup address = Cnd.exps(new Static(" param_data->>'address' like '%" + bean.getQueryCondition() + "%'"));
            SqlExpressionGroup createUserName = Cnd.exps(new Static("create_user_name like '%" + bean.getQueryCondition() + "%'"));
            criSql.and(placeName.or(placeType).or(address).or(createUserName));
        }
        return cri;
    }
}
